package cn.school.web;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.mysql.jdbc.log.LogUtils;

/**
 * Servlet Filter implementation class TimeFilter
 */
@WebFilter("/*")
public class TimeFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public TimeFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		long start = System.nanoTime();
		HttpServletRequest req = (HttpServletRequest) request;
		req.setCharacterEncoding("utf-8");
		// >>>response.setContentType("");
		String path = req.getServletPath();// 访问地址
		System.out.println("进入" + path + "的时间是:" + new java.util.Date());
		// 放行
		chain.doFilter(request, response);
		long end = System.nanoTime();
		System.out.println("离开" + path + "的时间是:" + new java.util.Date() + ",花费:" + (end - start) + "毫微秒");

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
